package com.zyzd.custom.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by xinggang_yin on 2018/4/27 0027.
 * description ${TODO}
 * <p>
 * Changed by somebody on 2018/4/27 0027
 * description ${TODO}
 */

public class TurnplateView extends View {

    private int mOutCircleRadius;
    private int mAnnulusThickness;
    private int mAnnulusPieNums = 60;
    private Paint mPaint;
    private int mPieNums = 8;


    private int mPaddingBottom;
    private int mPaddingTop;
    private int mPaddingLeft;
    private int mPaddingRight;
    private int mPadding;

    private int[] mFillColors = new int[]{Color.RED, Color.GREEN};
    private int[] mAnnulusPieColors = new int[]{Color.WHITE, Color.BLACK};
    private String[] mPieText = new String[]{"A", "B", "C", "D", "E", "F", "G", "H"};
    private int mMiddleCircleRadius;

    public TurnplateView(Context context) {
        this(context, null);
    }

    public TurnplateView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TurnplateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeight = MeasureSpec.getSize(heightMeasureSpec);

        int resultWidth = measureWidth > measureHeight ? measureHeight : measureWidth;
        setMeasuredDimension(resultWidth, resultWidth);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        getPadding();

        mOutCircleRadius = w / 2 - mPadding;
        mAnnulusThickness = mOutCircleRadius / 30;
        mMiddleCircleRadius = mOutCircleRadius - mAnnulusThickness;
    }

    private void getPadding() {
        mPadding = getPaddingTop() < getPaddingBottom() ? getPaddingTop() : getPaddingBottom();
        mPadding = getPaddingLeft() < mPadding ? getPaddingLeft() : mPadding;
        mPadding = getPaddingRight() < mPadding ? getPaddingRight() : mPadding;
    }

    /**
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mOutCircleRadius + mPadding, mOutCircleRadius + mPadding);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.RED);
        mPaint.setColor(Color.BLACK);

        drawCircleAnnulus(canvas);
        drawPie(canvas);
    }


    private int drawCircleAnnulus(Canvas canvas) {
        canvas.drawCircle(0, 0, mOutCircleRadius, mPaint);
        canvas.drawCircle(0, 0, mMiddleCircleRadius, mPaint);

        int radius = mOutCircleRadius - (mAnnulusThickness / 2);
        RectF outRectF = new RectF(-radius, -radius, radius, radius);
        float rotateDegress = 360f / mAnnulusPieNums;

        for (int i = 0; i < mAnnulusPieNums; i++) {
            mPaint.setStrokeWidth(mAnnulusThickness);
            mPaint.setColor(mAnnulusPieColors[i % 2]);
            canvas.drawArc(outRectF, -90, rotateDegress, false, mPaint);
            canvas.rotate(rotateDegress, 0, 0);
        }
        return mMiddleCircleRadius;
    }

    private void drawPie(Canvas canvas) {
        mPaint.setStrokeWidth(0);
        float sweepDegress = 360f / mPieNums;
        RectF innerRectF = new RectF(-mMiddleCircleRadius, -mMiddleCircleRadius, mMiddleCircleRadius, mMiddleCircleRadius);
        for (int i = 0; i < mPieNums; i++) {
            mPaint.setColor(mFillColors[i % mFillColors.length]);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawArc(innerRectF, -90 - sweepDegress / 2, sweepDegress, true, mPaint);

            mPaint.setColor(Color.BLACK);
            mPaint.setTextAlign(Paint.Align.CENTER);
            mPaint.setTextSize(mOutCircleRadius * 0.2f);
            canvas.drawText(mPieText[i], 0, -mMiddleCircleRadius * 0.7f, mPaint);

            canvas.rotate(sweepDegress);
        }
    }
}
